package com.jike.noobmoney.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MoneyEntity;
import com.jike.noobmoney.mvp.presenter.UserPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.AppUtils;
import com.jike.noobmoney.util.Logger;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Create by xuke on 2019/3/11
 * 提现中心
 */
public class TiXianCenterActivity extends BaseActivity implements IView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_mingxi)
    LinearLayout llMingxi;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_able_tixian)
    TextView tvAbleTixian;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    private String userid;
    private UserPresenter userPresenter;
    private MoneyEntity.UserBean userBean;


    @Override
    protected int layoutResID() {
        return R.layout.activity_tixian_center;
    }

    @Override
    protected void initData() {
        tvTitle.setText("提现中心");
        userPresenter = new UserPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        refreshData();
    }

    /**
     * 获取余额
     */
    private void refreshData() {
        showProgress();
        userPresenter.moneyView(userid, ConstantValue.RequestKey.getMoneyView);
    }

    @OnClick({R.id.iv_back, R.id.ll_mingxi, R.id.tv_tixian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_mingxi:
                ShouZhiActivity.startAction(context, 3);
                break;
            case R.id.tv_tixian:
                if (AppUtils.isFastClick()) {
                    return;
                }
                if (userBean != null) {
                    if (Math.floor(userBean.getMoney() / 108) < 0) {
                        ToastUtils.showShortToastSafe("可提现金额不足1元");
                        return;
                    }

                    Intent intent = new Intent(TiXianCenterActivity.this, TiXianActivity.class);
                    intent.putExtra(ConstantValue.IntentKey.ABLE_MONEY, Math.floor(userBean.getMoney() / 108));
                    startActivityForResult(intent, 100);
                }

                break;
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        dismissProgress();
        if (ConstantValue.RequestKey.getMoneyView.equals(actionType)) {
            //获取用户余额
            userBean = (MoneyEntity.UserBean) data;
            tvMoney.setText("" + userBean.getMoney());

            tvAbleTixian.setText("" + Math.floor(userBean.getMoney() / 108));
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        dismissProgress();
        ToastUtils.showShortToastSafe(msg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            Logger.e("xuke", "onActivityResult");
            refreshData();
        }
    }

}
